package com.ruitu.mall_98_2.order.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.WineRefreshHeader;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.widget.dialog.CustomAlertDialog;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.base.BaseFragment;
import com.ruitu.mall_98_2.mvp.contract.OrderContract;
import com.ruitu.mall_98_2.mvp.model.OrderModel;
import com.ruitu.mall_98_2.mvp.presenter.OrderPresenter;
import com.ruitu.mall_98_2.order.PayOrderActivity;
import com.ruitu.mall_98_2.order.WaitingPayOrderDetailsActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.order.ConfirOrder;
import com.ruitu.router_module.bean.order.Order;
import com.ruitu.router_module.bean.order.OrderList;
import com.ruitu.router_module.bean.order.SmallOrder;

public class WaitingPayFragment extends BaseFragment<OrderPresenter, OrderModel> implements OrderContract.View {
    private DataWaitingPayAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private Order order;
    private LRecyclerView lRecyclerView;
    private CustomAlertDialog customAlertDialog;
    private Intent intent;
    private String accountId = null;

    public static WaitingPayFragment newInstance(int position) {
        Bundle args = new Bundle();
        args.putInt("position", position);
        WaitingPayFragment fragment = new WaitingPayFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, null);
        lRecyclerView = view.findViewById(R.id.lRecyclerView);
        lRecyclerView.setRefreshHeader(new WineRefreshHeader(getActivity()));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        lRecyclerView.setLayoutManager(manager);
        adapter = new DataWaitingPayAdapter(getContext());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lRecyclerView.setAdapter(lRecyclerViewAdapter);
        lRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                reReq();
            }
        });
        if (TextUtils.isEmpty(AppHolder.instance().getUser().getAccountId())) {
            accountId = savedInstanceState.getString("accountId");
        } else {
            accountId = AppHolder.instance().getUser().getAccountId();
        }
        return view;
    }

    protected void reReq() {
        mPresenter.orderInfo(accountId);
    }

    @Override
    public void setData1(PageModel<SmallOrder> orders) {

    }

    @Override
    public void setData(Order order) {
        lRecyclerView.refreshComplete(20);
        this.order = order;
        adapter.setDataList(order.getOrderList());
        setData(isDataEmpty(order.getOrderList()), lRecyclerView);
    }

    @Override
    public void onResume() {
        super.onResume();
        reReq();
    }

    @Override
    public void setSuc() {
        reReq();
    }

    @Override
    public void setFail() {
        showError();
        lRecyclerView.refreshComplete(20);
    }

    @Override
    protected void getReData() {
        super.getReData();
//        showProgress();
        reReq();
    }

    class DataWaitingPayAdapter extends ListBaseAdapter<OrderList> {

        public DataWaitingPayAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_data_waiting_pay;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_company_name = holder.getView(R.id.tv_company_name);
            TextView tv_pay_model = holder.getView(R.id.tv_pay_model);
            TextView tv_actually_paid = holder.getView(R.id.tv_actually_paid);
            TextView tv_cancle_order = holder.getView(R.id.tv_cancle_order);
            TextView tv_waiting_pay = holder.getView(R.id.tv_waiting_pay);
            TextView tv_down_payment_status = holder.getView(R.id.tv_down_payment_status);
            TextView tv_down_payment = holder.getView(R.id.tv_down_payment);
            TextView tv_balance_payment = holder.getView(R.id.tv_balance_payment);
            TextView tv_balance_payment_status = holder.getView(R.id.tv_balance_payment_status);
            LinearLayout ll_advance_massege = holder.getView(R.id.ll_advance_massege);
            LinearLayout ll_btn = holder.getView(R.id.ll_btn);
            LinearLayout ll_need_pay = holder.getView(R.id.ll_need_pay);
            LinearLayout ll_balance_payment = holder.getView(R.id.ll_balance_payment);
            final OrderList orderList = mDataList.get(position);
            //当前为预售的订单状态车里
            if (TextUtils.equals(orderList.getOrderType(), "1") || TextUtils.equals("2", orderList.getOrderType())) {
                ll_need_pay.setVisibility(View.GONE);
                if (TextUtils.equals(orderList.getIsPayOver(), "2")) {//已经支付订金
                    ll_advance_massege.setVisibility(View.VISIBLE);//订金的
                    ll_balance_payment.setVisibility(View.VISIBLE);//尾款
                    ll_btn.setVisibility(View.GONE);
                    tv_down_payment_status.setText("已付订金:");
                    tv_balance_payment_status.setText("需付尾款:");
                    tv_down_payment.setText("￥" + orderList.getAdvanceMoney());
                    tv_balance_payment.setText("￥" + orderList.getTailMoney());
                    tv_pay_model.setText("制作中");
                } else if (TextUtils.equals(orderList.getIsPayOver(), "4")) {//等待支付尾款
                    ll_advance_massege.setVisibility(View.VISIBLE);
                    ll_balance_payment.setVisibility(View.VISIBLE);
                    ll_btn.setVisibility(View.VISIBLE);
                    tv_down_payment_status.setText("已付订金:");
                    tv_balance_payment_status.setText("需付尾款:");
                    tv_down_payment.setText("￥" + orderList.getAdvanceMoney());
                    tv_balance_payment.setText("￥" + orderList.getTailMoney());
                    tv_cancle_order.setVisibility(View.GONE);
                    tv_waiting_pay.setText("支付尾款");
                    tv_pay_model.setText("等待买家付尾款");

                } else if (TextUtils.equals(orderList.getIsPayOver(), "0")) {//待支付订金
                    tv_down_payment_status.setText("需付订金:");
                    tv_waiting_pay.setText("支付订金");
                    tv_balance_payment_status.setText("尾款:");
//                    ll_balance_payment.setVisibility(View.GONE);
                    tv_down_payment.setText("￥" + orderList.getAdvanceMoney());
                    tv_balance_payment.setText("￥" + orderList.getTailMoney());
                    ll_btn.setVisibility(View.VISIBLE);
                    tv_cancle_order.setVisibility(View.VISIBLE);
                    tv_pay_model.setText("等待买家付款");
                } else {
                    ll_advance_massege.setVisibility(View.GONE);
                    ll_btn.setVisibility(View.GONE);
                    tv_pay_model.setText("等待买家付款");
                }
            } else {
                ll_advance_massege.setVisibility(View.GONE);
                ll_need_pay.setVisibility(View.VISIBLE);
                ll_btn.setVisibility(View.VISIBLE);
                tv_cancle_order.setVisibility(View.VISIBLE);
                tv_waiting_pay.setText("去支付");
                tv_pay_model.setText("等待买家付款");
            }

            tv_actually_paid.setText("需付款:" + orderList.getOrderTotal() + "元");
            tv_company_name.setText(orderList.getCompanyName());
            RecyclerView recyclerView = holder.getView(R.id.recyclerView);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(manager);
            DataAdapter2 adapter2 = new DataAdapter2(mContext);
            recyclerView.setAdapter(adapter2);
            adapter2.setDataList(orderList.getOrderDetails());

            adapter2.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View itemView, int position) {
                    intent = new Intent(getContext(), WaitingPayOrderDetailsActivity.class);
                    intent.putExtra("orderList", orderList);
                    startActivity(intent);

                }
            });

            tv_cancle_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    customAlertDialog = new CustomAlertDialog(mContext, "确定取消订单吗", new CustomAlertDialog.OkOnClickListener() {
                        @Override
                        public void OkOnClick() {
                            mPresenter.cancelOrder(orderList.getId());
                            customAlertDialog.dismiss();
                        }
                    });
                    customAlertDialog.show();
                }
            });

            tv_waiting_pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ConfirOrder confirOrder = new ConfirOrder();
                    confirOrder.setId(orderList.getId());
                    confirOrder.setCollectGoodsAddress(orderList.getCollectGoodsAddress());
                    confirOrder.setCollectGoodsPhone(orderList.getCollectGoodsPhone());
                    confirOrder.setConsigneeName(orderList.getConsigneeName());
                    confirOrder.setOrderProvinceCode(orderList.getOrderProvinceCode());
                    confirOrder.setOrderCityCode(orderList.getOrderCountyCode());
                    confirOrder.setOrderCountyCode(orderList.getOrderCountyCode());
                    confirOrder.setOrderTotal(orderList.getOrderTotal());
                    confirOrder.setOrderType(orderList.getOrderType());
                    confirOrder.setIsPayOver(orderList.getIsPayOver());
                    intent = new Intent(mContext, PayOrderActivity.class);
                    intent.putExtra("confirOrder", confirOrder);
                    startActivity(intent);
                }
            });
        }
    }

    class DataAdapter2 extends ListBaseAdapter<OrderList.OrderDetailsBean> {

        public DataAdapter2(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_data_waiting_pay_commodity;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            ImageView iv_order_type = holder.getView(R.id.iv_order_type);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            TextView tv_need_pay = holder.getView(R.id.tv_need_pay);
            LinearLayout ll_xiajia = holder.getView(R.id.ll_xiajia);
            OrderList.OrderDetailsBean orderDetailsBean = mDataList.get(position);
            SpannableString spannableString = new SpannableString("   " + orderDetailsBean.getGoodsName());

            Drawable drawable = null;
            if (!TextUtils.isEmpty(orderDetailsBean.getOrderType())) {
                switch (orderDetailsBean.getOrderType()) {
                    case "3":
                        drawable = mContext.getResources().getDrawable(R.mipmap.collage);
                        break;
                    case "2":
                        drawable = mContext.getResources().getDrawable(R.mipmap.reserve);
                        break;
                    case "1":
                        drawable = mContext.getResources().getDrawable(R.mipmap.presale);
                        break;
                    default:

                        break;
                }
            }
            if (TextUtils.equals(orderDetailsBean.getGoodStatus(),"0")){
                ll_xiajia.setVisibility(View.VISIBLE);
            }else if (TextUtils.equals(orderDetailsBean.getGoodStatus(),"1")){
                ll_xiajia.setVisibility(View.GONE);
            }
            ImageSpan imageSpan = new ImageSpan(drawable);
            if (drawable == null) {
                tv_commodity_name.setText(orderDetailsBean.getGoodsName());
            } else {
                drawable.setBounds(0, 0, 100, 50);
                spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                tv_commodity_name.setText(spannableString);
            }
            Glide.with(mContext).load(Config.BASE_URL + orderDetailsBean.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            tv_commodity_info.setText(orderDetailsBean.getSkuContent());
            tv_commodity_num.setText("x" + orderDetailsBean.getQuantity());
            tv_commodity_price.setText("￥" + orderDetailsBean.getGoodsPrice());
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(accountId, "accountId");
    }
}
